package net.ib.mtalk.network.task;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.BuddyInfo;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Common.UUIDSlicePredicate;
import net.ib.mntalk.protocol.Common.UUIDSliceRange;
import net.ib.mntalk.protocol.File;
import net.ib.mntalk.protocol.Session.GetBuddyInfosReq;
import net.ib.mntalk.protocol.Session.GetBuddyInfosRes;
import net.ib.mntalk.protocol.Session.GetContactsRes;
import net.ib.mntalk.protocol.Session.GetViewsReq;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.BuddyDatabaseHelper;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.ui.buddy.talk.BuddyTalkListActivity;
import net.ib.mtalk.ui.inbox.InboxListActivity;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.GraphicUtil;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;
import net.ib.mtalk.util.ProfileImageLoader;

public class GetBuddyInfosTask extends AsyncNetTask {
	
	public static int GET_BUDDY_MAX_COUNT = 100;
	
	private GetBuddyInfosReq getBuddyInfosReq;

	public GetBuddyInfosTask(Context _context, Handler _uiHandler, GetBuddyInfosReq getBuddyInfosReq) {
		super(_context, _uiHandler);
		this.getBuddyInfosReq = getBuddyInfosReq;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(getBuddyInfosReq)
			.setMethodId(Session.Contact.GetBuddyInfos)
			.build();
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("GetBuddyInfosTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetBuddyInfosRes getBuddyInfosRes = (GetBuddyInfosRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("GetBuddyInfosTask ResponseCode : " + getBuddyInfosRes.getResponseCode());
		
		if(getBuddyInfosRes.getResponseCode() == Common.SUCCESS){
			MTalkLogWriter.d("getBuddyInfosRes.getBuddyInfoCount() = " + getBuddyInfosRes.getBuddyInfoCount());
			BuddyDatabaseHelper.insertGetBuddies(mContext, getBuddyInfosRes);
			Message msg = mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_BUDDY_INFOS_SUCCESS);
			mUiHandler.sendMessageAtFrontOfQueue(msg);
			for(int i=0; i < getBuddyInfosRes.getBuddyInfoCount(); i++){
				BuddyInfo info = getBuddyInfosRes.getBuddyInfo(i);
				if(info.getProfile().getProfileImage() == null) {
					BuddyDatabaseHelper.updateBuddyImage(mContext, info.getUserUuid(), true);
					continue;
				}
				if(info.getProfile().getProfileImage().getFileName().equals("null")){
					ProfileImageLoader.getInstance(mContext).deleteImage(info.getUserUuid());
					continue;
				}
				ProfileDownloadTask task = new ProfileDownloadTask(mContext, mUiHandler, info.getUserUuid(), 
										info.getProfile().getProfileImage(),
										null,
//										new File.ImageSize().setHeight(GraphicUtil.IMAGE_SIZE_BUDDY_PROFILE).setWidth(GraphicUtil.IMAGE_SIZE_BUDDY_PROFILE), 
										-1, 1);
				task.execute();
			}
		} else {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_BUDDY_INFOS_FAIL));
		}
	}

}
